package com.atguigu.customer.controller;


import com.atguigu.customer.bean.Customer;
import com.atguigu.customer.service.CustomerService;
import com.atguigu.customer.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController   //返回的是字符串值
//@Controller  带页面的接口
public class CustomerController {

    @Autowired
    @Qualifier("customer1")
    CustomerService customerService;

    @RequestMapping("/sayhello")
    public String sayHello(){
        return  "hello world !!!";
    }

    @GetMapping("/customer")
    public List<Customer>  customer(@RequestParam("name") String name ,@RequestParam(value = "age",required = false) Integer age){
        //List<Customer> customerList = customerService.getCustomerByName(name);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>().like(name != null, "name", name);
        List<Customer> customerList = customerService.list(queryWrapper);
        return customerList;
    }

    @RequestMapping("/customer/{id}")
    public String getCustomerById( @PathVariable("id") Integer id){
        //Customer customer = customerService.getCustomerById(id);
        Customer customer = customerService.getById(id);
        return   " hello world :::   :"+customer;
    }


    @PostMapping("/customer")
    public  String saveCustomer(@RequestBody Customer customer){

        //customerService.saveCustomer(customer);
        customerService.saveOrUpdate(customer);
        return "hello world :::  name:"+customer.getName()+" age:"+customer.getAge();
    }

    // 补充修改

    // 补充删除

}
